package com.demo.module.service.impl;

import com.demo.module.service.RedMoneyService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class RedMoneyServiceImpl implements RedMoneyService {

    private Map<String, List<Integer>> map = new ConcurrentHashMap();

    @Override
    public void handRed(String userId ,BigDecimal totalAmount, Integer num) {
        roandomMoney(userId ,totalAmount, num);
    }


    @Override
    public BigDecimal rob(Integer userId, String redId) {
        List<Integer> integers = map.get(userId);
        if (CollectionUtils.isEmpty(integers)) {
            return null;
        }

        return null;
    }


    private void roandomMoney(String userId ,BigDecimal totalAmount, Integer num) {
        //计算红包数量
        List<Integer> userReds = new ArrayList<>();
        Random random = new Random();
        BigDecimal reSetAmount = totalAmount;
        Integer reSetPerson = num;

        for (int i = 0; i < reSetPerson; i++) {
            int i1 = (reSetAmount.divide(new BigDecimal(reSetPerson * 2)).subtract(new BigDecimal(1))).intValue();
            int amount = random.nextInt(i1) + 1;
            reSetAmount = reSetAmount.subtract(new BigDecimal(amount));
            reSetPerson--;
            userReds.add(i1);
        }
        //暂时放在map
        map.putIfAbsent(userId,userReds);
    }
}
